package com.itclj.common.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Redis客户端
 * 
 * @author  lujun.chen
 * @version  2016年11月15日
 * @since  JDK1.8
 */
public class RedisUtil {

	private static String ADDR = "127.0.0.1";

	private static int PORT = 6379;

	private static String AUTH = null;

	private static int MAX_ACTIVE = 300;

	private static int MAX_IDLE = 200;

	private static int MAX_WAIT = 10000;

	private static int TIMEOUT = 10000;

	private static boolean TEST_ON_BORROW = true;

	private static JedisPool jedisPool = null;

	private static Jedis jedis = null;

	/**
	 * 初始化Redis连接池
	 */
	static {
		try {
			init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 初始化连接池
	 * 
	 * @see [类、类#方法、类#成员]
	 */
	private synchronized static void init() {
		ADDR = PropertyUtil.getValue("redis.addr");
		PORT = Integer.valueOf(PropertyUtil.getValue("redis.port"));
		AUTH = PropertyUtil.getValue("redis.auth");
		MAX_ACTIVE = Integer.valueOf(PropertyUtil.getValue("redis.pool.max_active"));
		MAX_IDLE = Integer.valueOf(PropertyUtil.getValue("redis.pool.max_idle"));
		MAX_WAIT = Integer.valueOf(PropertyUtil.getValue("redis.pool.max_wait"));
		TIMEOUT = Integer.valueOf(PropertyUtil.getValue("redis.pool.timeout"));

		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(MAX_IDLE);
		config.setMaxWaitMillis(MAX_WAIT);
		config.setTestOnBorrow(TEST_ON_BORROW);
		config.setMaxTotal(MAX_ACTIVE);
		jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
	}

	/**
	 * 获取Jedis实例
	 * 
	 * @return
	 */
	private static Jedis getJedis() {
		try {
			if (jedisPool != null) {
				jedis = jedisPool.getResource();
			} else {
				init();
				jedis = jedisPool.getResource();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jedis;
	}

	/**
	 * 设置单个值
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static String set(String key, String value) {
		return set(key, value, null);
	}

	/**
	 * 设置单个值，并设置超时时间
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param timeout
	 *            超时时间（秒）
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String set(String key, String value, Integer timeout) {
		String result = null;

		Jedis jedis = RedisUtil.getJedis();
		if (jedis == null) {
			return result;
		}
		try {
			result = jedis.set(key, value);
			if (null != timeout) {
				jedis.expire(key, timeout);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != jedis) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 获取单个值
	 * 
	 * @param key
	 * @return
	 */
	public static String get(String key) {
		String result = null;
		Jedis jedis = RedisUtil.getJedis();
		if (jedis == null) {
			return result;
		}
		try {
			result = jedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != jedis) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 删除redis中数据
	 * 
	 * @param key
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static boolean del(String key) {
		Boolean result = Boolean.FALSE;
		Jedis jedis = RedisUtil.getJedis();
		if (null == jedis) {
			return Boolean.FALSE;
		}
		try {
			jedis.del(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != jedis) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 追加
	 * 
	 * @param key
	 * @param value
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static Long append(String key, String value) {
		Long result = Long.valueOf(0);
		Jedis jedis = RedisUtil.getJedis();
		if (null == jedis) {
			return result;
		}
		try {
			result = jedis.append(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != jedis) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 检测是否存在
	 * 
	 * @param key
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static Boolean exists(String key) {
		Boolean result = Boolean.FALSE;
		Jedis jedis = RedisUtil.getJedis();
		if (null == jedis) {
			return result;
		}
		try {
			result = jedis.exists(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != jedis) {
				jedis.close();
			}
		}
		return result;
	}
}
